package com.odriver.neulearn.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.odriver.neulearn.Bean.MenuBean;
import com.odriver.neulearn.R;
import com.odriver.neulearn.Util.TransferUtil;

import java.util.List;

/**
 * Created by yeruyi on 2016/11/28.
 */

public class MenuListviewAdapter extends BaseAdapter {

    private List<MenuBean.IndexPositionListBean> data;
    private LayoutInflater inflate;
    private Context context;

    public MenuListviewAdapter(Context context, List<MenuBean.IndexPositionListBean> data) {
        this.data = data;
        this.context = context;
        inflate = LayoutInflater.from(context);
    }

    public void updateData(List<MenuBean.IndexPositionListBean> data) {
        if (this.data != null) {
            this.data.clear();
        }
        this.data = data;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflate.inflate(R.layout.item_menu_listview, null);
            viewHolder.pic = (ImageView) convertView.findViewById(R.id.idp_item_menu_listview_pic);
            viewHolder.studyTopic = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_topic);
            viewHolder.studyNum = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_classNum);
            viewHolder.studyDayNum = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_classDayNum);
            viewHolder.studyPeople = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_personNum);
            viewHolder.studyTime = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_classTime);
            viewHolder.studyTest = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_classTest);
            viewHolder.studyVideo = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_videoNum);
            viewHolder.studyInfo = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_info);
            viewHolder.id = (TextView) convertView.findViewById(R.id.idp_item_menu_listview_id);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MenuBean.IndexPositionListBean map = (MenuBean.IndexPositionListBean) this.getItem(position);

        TransferUtil.getBitmap(context, map.getPostUrl(), viewHolder.pic);
        viewHolder.studyTopic.setText(map.getPostName());
        viewHolder.studyNum.setText("课程数目：" + map.getCourseNums());
        viewHolder.studyDayNum.setText("课程天数：" + map.getStudyDays());
        viewHolder.studyPeople.setText("参课人数：" + map.getPersonNums());
        viewHolder.studyTime.setText("课程学时：" + map.getCourseHours());
        viewHolder.studyTest.setText("课程测验：" + map.getTestNums());
        viewHolder.studyVideo.setText("视频总数：" + map.getVideoNums());
        viewHolder.studyInfo.setText(map.getPostDesc().replace("&nbsp;"," "));
        viewHolder.id.setText("课程ID："+ map.getPostId());
        return convertView;
    }

    static class ViewHolder {
        TextView studyNum, studyDayNum, studyPeople, studyTime, studyTest, studyVideo, studyTopic, studyInfo,id;
        ImageView pic;
        Button button;
    }
}
